<?php

namespace App\Api\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ProductResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            'data_id' => $this->data_id,
            'product_category' => $this->product_category,
            'product_category_text' => $this->product_category_text,
            'product_name' => $this->product_name,
            'product_price' => $this->product_price,
            'product_discount_price' => $this->product_discount_price,
            'product_info' => $this->product_info,
            'product_thumb' => $this->product_thumb,
            'product_enter_pic' => $this->product_enter_pic,
            'product_banner' => $this->product_banner,
            'product_detail_pic' => $this->product_detail_pic,
            'product_sort' => $this->product_sort,
            'product_stock' => $this->product_stock,
            'product_unit' => $this->product_unit,
            'product_tag' => $this->product_tag,
            'product_sku' => $this->product_sku,
            'product_is_hot' => $this->product_is_hot,
            'product_vip_price' => $this->product_vip_price,
            'create_time' => $this->create_time->toDateTimeString(),
            'service_period' =>$this->when($this->product_category == 3, function () {
                return $this->service->format_service_period ?? '';
            }),
            'sales' =>  $this->when(isset($this->sales), function () {
                return $this->sales->sum('product_num') ?? 0;
            }), // 销量
            'num' => $this->when(isset($this->num), function () {
                return $this->num;
            })
        ];
    }
}
